package com.ruoyi.project.system.auth.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.pac4j.Pac4jService;
import com.ruoyi.project.system.auth.domain.SysAuth;
import com.ruoyi.project.system.auth.mapper.AuthMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author renzhihao
 * @date 2023/10/12 15:58
 */
@Service
public class AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Autowired
    private Pac4jService pac4jService;

    public List<SysAuth> selectAuthList(SysAuth sysAuth) {
        return authMapper.selectAuthList(sysAuth);
    }


    public int insertAuth(SysAuth sysAuth) {
        return authMapper.insertAuth(sysAuth);
    }

    public SysAuth selectAuthById(Long authId) {
        return authMapper.selectById(authId);
    }

    public int updateAuth(SysAuth sysAuth) {
        sysAuth.setUpdateBy(ShiroUtils.getLoginName());
        int result = authMapper.updateAuth(sysAuth);
        pac4jService.init();
        return result;
    }

    public int changeStatus(SysAuth sysAuth) {
        sysAuth.setUpdateBy(ShiroUtils.getLoginName());
        int result = authMapper.updateAuth(sysAuth);
        pac4jService.init();
        return result;
    }

    public int copyByIds(String ids) {
        Long[] authIds = Convert.toLongArray(ids);

        int result = 0;
        for (long id : authIds) {
            SysAuth sysAuth = selectAuthById(id);
            sysAuth.setAuthId(null);
            sysAuth.setStatus(1);
            sysAuth.setAuthName(sysAuth.getAuthName() + "_1");
            sysAuth.setClientName(sysAuth.getClientName() + "_1");
            if (StringUtils.isNotEmpty(sysAuth.getClientId())) {
                sysAuth.setClientId(sysAuth.getClientId() + "_1");
            }

            if (StringUtils.isNotEmpty(sysAuth.getEntityId())) {
                sysAuth.setEntityId(sysAuth.getEntityId() + "_1");
                sysAuth.setClientId("sp-metadata_" + sysAuth.getEntityId() + ".xml");
            }
            sysAuth.setCreateBy(ShiroUtils.getLoginName());
            sysAuth.setUpdateBy(null);
            result += insertAuth(sysAuth);
        }

        return result;
    }

    public int deleteAuthByIds(Long[] ids) {
        int result = authMapper.deleteAuthByIds(ids);
        pac4jService.init();
        return result;
    }

    public List<SysAuth> getEnabledAuthWay() {
        SysAuth sysAuth = new SysAuth();
        sysAuth.setStatus(0);
        return authMapper.selectAuthList(sysAuth);
    }

    public SysAuth getAuthWayByClientName(String clientName) {
        SysAuth sysAuth = new SysAuth();
        sysAuth.setClientName(clientName);
        List<SysAuth> sysAuths = authMapper.selectAuthList(sysAuth);
        if (CollectionUtils.isNotEmpty(sysAuths)) {
            return sysAuths.get(0);
        }
        return null;
    }

    public List<SysAuth> selectByIds(Long[] authIds) {
        return authMapper.selectByIds(authIds);
    }

    public boolean checkAuthNameUnique(SysAuth sysAuth) {
        Long authId = StringUtils.isNull(sysAuth.getAuthId()) ? -1L : sysAuth.getAuthId();
        SysAuth info = authMapper.checkAuthNameUnique(sysAuth.getAuthName());
        if (StringUtils.isNotNull(info) && info.getAuthId().longValue() != authId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public boolean checkClientNameUnique(SysAuth sysAuth) {
        Long authId = StringUtils.isNull(sysAuth.getAuthId()) ? -1L : sysAuth.getAuthId();
        SysAuth info = authMapper.checkClientNameUnique(sysAuth.getClientName());
        if (StringUtils.isNotNull(info) && info.getAuthId().longValue() != authId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public boolean checkMetadataFilenameUnique(SysAuth sysAuth) {
        Long authId = StringUtils.isNull(sysAuth.getAuthId()) ? -1L : sysAuth.getAuthId();
        SysAuth info = authMapper.checkMetadataFilenameUnique(sysAuth.getMetadataFilename());
        if (StringUtils.isNotNull(info) && info.getAuthId().longValue() != authId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public boolean existByKeystoreIds(Long[] keystoreIds) {
        int count = authMapper.countByKeystoreIds(keystoreIds);
        return count > 0;
    }
}
